package com.hhit.zhgd.service.impl

import cn.afterturn.easypoi.excel.ExcelImportUtil
import cn.afterturn.easypoi.excel.entity.ImportParams
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.hhit.zhgd.dao.ZhgdArchiveSituationsMapper
import com.hhit.zhgd.domain.entity.ZhgdArchiveSituations
import com.hhit.zhgd.domain.excelimport.ArchiveSituationsImportParam
import com.hhit.zhgd.domain.response.BaseResp
import com.hhit.zhgd.service.ZhgdArchiveSituationsService
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile

@Service("zhgdArchiveSituationsService")
class ZhgdArchiveSituationsServiceImpl : ZhgdArchiveSituationsService,ServiceImpl<ZhgdArchiveSituationsMapper, ZhgdArchiveSituations>() {
    override fun importArchivesFile(multipartFile: MultipartFile?): BaseResp<Any?>? {
        this.remove(QueryWrapper())
        val params = ImportParams()
        // 表头设置为1行
        params.headRows = 1
        val result: ExcelImportResult<ArchiveSituationsImportParam> =
            ExcelImportUtil.importExcelMore(
                multipartFile!!.inputStream,
                ArchiveSituationsImportParam::class.java, params
            )
        val list: List<ArchiveSituationsImportParam> = result.list

        val zhgdArchiveSituations: MutableList<ZhgdArchiveSituations> = ArrayList()

        // 补全缺失的一级目录
        var lmh = StrUtil.EMPTY
        var yjlmmc = StrUtil.EMPTY
        for (archiveSituationsImportParam in list) {
            if (StrUtil.isNotBlank(archiveSituationsImportParam.lmh)) {
                lmh = archiveSituationsImportParam.lmh?:""
            }
            if (StrUtil.isNotBlank(archiveSituationsImportParam.yjlmmc)) {
                yjlmmc = archiveSituationsImportParam.yjlmmc?:""
            }
            archiveSituationsImportParam.lmh = lmh
            archiveSituationsImportParam.yjlmmc = yjlmmc
            val zhgdArchiveSituations1 = ZhgdArchiveSituations()
            BeanUtils.copyProperties(archiveSituationsImportParam, zhgdArchiveSituations1)
            zhgdArchiveSituations.add(zhgdArchiveSituations1)
        }
        this.saveBatch(zhgdArchiveSituations)

        return BaseResp.success()
    }

    override fun getArchivesFileTree(): BaseResp<Any?>? {
        TODO("Not yet implemented")
    }
}